﻿//* Write a program that converts a number in the range [0...999] to a text corresponding to its English pronunciation. 
// Examples:
//	0   -> "Zero"
//	273 -> "Two hundred seventy three"
//	400 -> "Four hundred"
//	501 -> "Five hundred and one"
//	711 -> "Seven hundred and eleven"

using System;
using System.Text;

class ConvertNumberToText
{
    static void Main()
    {
        Console.Write("Enter number [0-999] to convert to text: ");
        string numberString = Console.ReadLine();
        int number;
        while (
            !(int.TryParse(numberString, out number) &&
            number >= 0 &&
            number <= 999)
            )
        {
            Console.Write("Incorrect input. Enter number [0-999] again: ");
            numberString = Console.ReadLine();
        }
        string numberText = ToText(number);
        if (!string.IsNullOrEmpty(numberText))
        {
            numberText = numberText.Substring(0, 1).ToUpper() + numberText.Substring(1);
        }
        Console.WriteLine("{0} -> {1}", number, numberText);
    }

    static string[] first = 
    { 
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
        "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
        "seventeen", "eighteen", "nineteen"
    };

    static string[] tens = 
    {
        "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"
    };

    public static string ToText(int number)
    {
        if (number <= 19)
        {
            return first[number];
        }
        else if (number <= 99)
        {
            string rest = "";
            if (number % 10 != 0)
            {
                rest = ToText(number % 10);
            }
            return tens[number / 10 - 2] + " " + rest;
        }
        else if (number <= 999)
        {
            int first = number % 100;
            string rest = "";
            string and = " ";
            if (first != 0)
            {
                rest = ToText(first);
                if (first < 20 || first % 10 == 0)
                {
                    and = " and ";
                }
            }
            return ToText(number / 100) + " hundred" + and + rest;
        }
        return "Out of scope !";
    }
}
